import tkinter as tk
from PIL import Image, ImageTk, ImageDraw
from .Shared import *                

# 登录界面
class PageStart(tk.Frame):

    def __init__(self, parent):
        super().__init__(parent)
        #界面属性初始化
        self.configure(bg = "black", width = 1200, height = 720, highlightthickness = 0)
        self.pack_propagate(False)

        #加载图像资源
        self.picture = {
            "PageStart": shareddata.get_picture("PageStart")
        }
        #初始化缓冲画布
        self.buffer_image = Image.new("RGBA", (1200, 720), (255, 255, 255, 0))
        self.canvas = tk.Canvas(self, width = 1200, height = 720, highlightthickness = 0, bd = 0)
        self.canvas.place(x = 0, y = 0)
        self.xa, self.xb, self.anm = 0, 1200, 0

        # 输入框
        self.entry = tk.Entry(self, font = ("Arial", 50), justify="center")
        self.entry.place(x = 350, y = 500, width = 500, height = 70)
        # 按钮
        self.canvas_gobutton = tk.Canvas(self, bg = "black", width = 300, height = 100, highlightthickness = 0, bd = 0)
        self.canvas_gobutton.place(x = 450, y = 590)
        self.image_gobutton_id = self.canvas_gobutton.create_image(0, 0, anchor = tk.NW, image = self.picture["PageStart"]["GoButtonOut"][1])
        self.canvas_gobutton.create_text(145, 43, anchor = tk.CENTER, text = 'GO', font = ("Algerian", 35, "italic"), fill = "lightgreen")
        self.canvas_gobutton.bind("<Enter>", self.mouse_enter)
        self.canvas_gobutton.bind("<Leave>", self.mouse_leave)
        self.canvas_gobutton.bind("<Button-1>", lambda event: self.mouse_click(event, parent))

        self.Animation(parent)
        self.update_buffer(parent)  

    def Animation(self, parent):
        self.xa -= 5
        self.xb -= 5
        if self.xa <= -1200:
            self.xa = 1200
        if self.xb <= -1200:
            self.xb = 1200
        self.anm = self.anm + 1
        self.update_buffer(parent)  
        self.after(33, lambda: self.Animation(parent))

    def mouse_enter(self, event):
        self.canvas_gobutton.itemconfig(self.image_gobutton_id, image=self.picture["PageStart"]["GoButtonIn"][1])

    def mouse_leave(self, event):
        self.canvas_gobutton.itemconfig(self.image_gobutton_id, image=self.picture["PageStart"]["GoButtonOut"][1])

    def mouse_click(self, event, parent):
        neckname = self.entry.get()
        shareddata.set_neckname(neckname)
        parent.Initialization()
        parent.init_framesB()
        parent.show_frame("PageHome")

    def update_buffer(self, parent):
        self.buffer_image.paste((255, 255, 255, 0), [0, 0, 1200, 720])
        self.draw_to_buffer(self.buffer_image, parent)
        self.photo_buffer = ImageTk.PhotoImage(self.buffer_image)
        self.canvas.create_image(0, 0, image = self.photo_buffer, anchor = tk.NW)

    def draw_to_buffer(self, buffer_image,parent):
        draw = ImageDraw.Draw(buffer_image)

        draw.rectangle([0, 0, 1200, 720], fill = (255, 255, 255, 0)) 
        buffer_image.paste(self.picture["PageStart"]["BackGround"][0], (self.xa, 0))  
        buffer_image.paste(self.picture["PageStart"]["BackGround"][0], (self.xb, 0))  
        buffer_image.paste(self.picture["PageStart"]["Title"][0], (12, 130), self.picture["PageStart"]["Title"][0]) 
        #获取帧动画时由于动画卡顿，所以为了动画流畅首尾几帧不进行绘制
        buffer_image.paste(self.picture["PageStart"]["Animation"][self.anm % 25 + 3][0], (300, 210), self.picture["PageStart"]["Animation"][self.anm % 25 + 3][0]) 